data = [333,1,7,12,54,81,1221]
for i in range(len(data)):
    for j in range(len(data)):
        if data[i] % 3 < data[j] % 3:
            data[i],data[j] = data[j],data[i]
for i in range(len(data)-1):
    if data[i] % 3 == 0 == data[i+1] % 3 == 0:
        if data[i] > data[i+1]:
            data[i],data[i+1] = data[i+1],data[i]
print(data)